package com.ja.openchat.ui.contact;

import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;

import com.chad.library.adapter.base.BaseSectionQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.google.gson.JsonObject;
import com.ja.openchat.R;
import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.manager.TgResultBlock;
import com.zy.openchat.core.model.contact.Section;
import com.zy.openchat.core.model.contact.SectionInfo;
import com.zy.openchat.core.model.message.ChatInfo;
import com.zy.openchat.core.model.user.UserInfo;
import com.ja.openchat.ui.chat.ChatActivity;
import com.ja.openchat.views.UserAvatarView;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Map;

public class MobileContactListAdapter extends BaseSectionQuickAdapter<Section, BaseViewHolder> {
    public MobileContactListAdapter(@Nullable List<Section> data) {
        super(R.layout.adapter_section_contact_list, data);
        setNormalLayout(R.layout.adapter_mobile_contact_list);
    }
    private int chooseType = 0;
    public void setChooseType(int chooseType){
        this.chooseType = chooseType;
    }

    @Override
    protected void convertHeader(@NotNull BaseViewHolder helper, @NotNull Section section) {
        helper.setText(R.id.iv_title, ((SectionInfo) section.getObject()).title);
    }

    @Override
    protected void convert(@NotNull BaseViewHolder helper, Section section) {
        UserInfo user = (UserInfo) section.getObject();
        helper.setText(R.id.iv_title, user.displayName());

        UserAvatarView avatarView = helper.getView(R.id.iv_icon);
        avatarView.setTextSize(17);
        avatarView.showUser(user);
        if(chooseType == 0){
            helper.setGone(R.id.check_box,true);
            helper.setGone(R.id.openchat_name_text,false);
            helper.setGone(R.id.add_btn,user.is_contact);
            helper.setText(R.id.openchat_name_text,String.format("Gochat：%s", TextUtils.isEmpty(user.realDisplayName)?user.displayName():user.realDisplayName));
            helper.getView(R.id.rootView).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    TelegramManager.getTelegramManager().createPrivateChat(getContext(), user.id, new TgResultBlock<ChatInfo>() {
                        @Override
                        public void onResult(Map<String, Object> request, JsonObject response, ChatInfo obj) {
                            if (obj != null) {
                                ChatActivity.startActivity(getContext(), obj);
                            }
                        }

                        @Override
                        public void onTimeout(Map<String, Object> request) {
                        }
                    });
                     }
            });
        }else{
            helper.setGone(R.id.openchat_name_text,true);
            helper.setGone(R.id.add_btn,true);
            if(chooseType==2||chooseType == -2||chooseType == 3){
                helper.setGone(R.id.check_box,false);
                CheckBox check_box = helper.getView(R.id.check_box);
                check_box.setChecked(section.isChecked());
            }else{
                helper.setGone(R.id.check_box,true);
            }
        }

    }
}
